package com.yeswater.multithread.chapter2.test2_2.test3;

import java.util.concurrent.TimeUnit;

/**
 * author : yeswater
 * date : 2023/10/20
 *
 * 在 synchronized 代码块中的是同步的 synchronized 代码块外是异步的
 */

public class Task {
    private String getData1;
    private String getData2;

    public void doLongTimeTask() {
        try {
            System.out.println("begin task");
            TimeUnit.SECONDS.sleep(3);
            String privateGetData1 = " 长时间处理任务后从远程返回的值 1 threadName=" + Thread.currentThread().getName();
            String privateGetData2 = " 长时间处理任务后从远程返回的值 2 threadName=" + Thread.currentThread().getName();

            synchronized (this) {
                getData1 = privateGetData1;
                getData2 = privateGetData2;
                System.out.println(getData1);
                System.out.println(getData2);
                System.out.println("end task");
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
